﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-09>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：井信息维护
//********************************************************************************//

using System;
using System.Data;
using System.Web.UI;
using BLL;
using BLL.Access;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS._2.YongHuGL
{
    public partial class JingXinXWH : BasePage
    {
        #region 属性

        /// <summary>
        ///     gridView主键(用户信息ID)
        /// </summary>
        private int JINGXXID
        {
            get { return Convert.ToInt32(ViewState["_JINGXXID"]); }
            set { ViewState["_JINGXXID"] = value; }
        }

        /// <summary>
        ///     保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get { return ViewState["dtSource"] as DataTable; }
            set { ViewState["dtSource"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     用户信息BLL
        /// </summary>
        private readonly QS_YG_YONGHUXXBLL _QS_YG_YONGHUXXBLL = new QS_YG_YONGHUXXBLL();

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private SpHelper _spHelper = new SpHelper();

        /// <summary>
        ///     深井信息
        /// </summary>
        private QS_YH_SHENJINGXXB _SJXX = new QS_YH_SHENJINGXXB();

        /// <summary>
        ///     深井信息BLL
        /// </summary>
        private readonly QS_YH_SHENJINGXXBBLL _SJXXBLL = new QS_YH_SHENJINGXXBBLL();


        private readonly QS_JX_XINGZHENGQBLL XZQ = new QS_JX_XINGZHENGQBLL();

        private readonly QS_JX_JINGBIAOJXXBLL JBJ = new QS_JX_JINGBIAOJXXBLL();


        private readonly QS_JX_LIUYUXXBLL LYXX = new QS_JX_LIUYUXXBLL();

        private readonly QS_JX_SHUILIFPXXBLL SLFP = new QS_JX_SHUILIFPXXBLL();

        private readonly QS_JX_SHUIZIYFQBLL SZYFQ = new QS_JX_SHUIZIYFQBLL();


        /// <summary>
        ///     日志BLL
        /// </summary>
        private readonly SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
            }
        }

        /// <summary>
        ///     重写Render方法
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }


        /// <summary>
        ///     放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Init;
            Display();
            //如果当前有操作项 重新加载数据
            if (JINGXXID > 0)
                SetData();
        }

        /// <summary>
        ///     修改
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtShenJingBH.Text == "")
            {
                PageTools.AlertMsg(this, "深井编号为空,请输入！");
            }
            else
            {
                var dt = _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(
                    string.Format("I_JLZT=0 and S_JINGBH='{0}'", txtShenJingBH.Text.Trim()), null);
                if (dt == null || dt.Rows.Count < 1)
                {
                    JINGXXID = -1;
                    PageTools.AlertMsg(this, "该井号不存在,请重新输入！");
                }
                else
                {
                    JINGXXID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    Status = GridViewStatus.Update;
                    Display();
                    SetData();
                }
            }
        }

        /// <summary>
        ///     保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
                SaveData();
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化控件
        /// </summary>
        private void InitControl()
        {
            txtYongHuH.Text = "";
            txtYONGHUM.Text = "";
            txtYuanShenJBH.Text = "";
            txtQuShuiDD.Text = "";
            cmbXingZhengQ.Text = "";
            cmbLiuYu.Text = "";
            cmbShuiZiYFQ.Text = "";
            cmbShuiLiFP.Text = "";
            cmbHuanXianBJ.Text = "";
            txtCengCi.Text = "";
            txtShenDu.Text = "";
            txtKouJing.Text = "";
            txtXuKeZ.Text = "";
            txtDu.Text = "";
            txtFen.Text = "";
            txtMiao.Text = "";
            txtDu2.Text = "";
            txtFen2.Text = "";
            txtMiao2.Text = "";
            cmbJingLei.Text = "";
            cmbJingBJ.Text = "";
            cmbHuanXianBJ.Text = "";
            cmbJingXZ.Text = "";
            txtBeiZhu.Text = "";
            txtXuKeSLH.Text = "";
        }

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            //加载行政区
            LoadXZQ();
            //加载环线标记
            LoadHXBJ();
            //加载井标记
            LoadJBJ();
            //加载流域
            LoadLY();
            //水利分片
            LoadSLFP();
            //水资源分区
            LoadSZYFQ();

            deRiQi_QS.Value = DateTime.Now;
            deRiQi_ZZ.Value = DateTime.Now;

            var jbh = Request.QueryString["jbh"];
            if (jbh != "" && jbh != null)
            {
                txtShenJingBH.Text = jbh;
                btnEdit_Click(null, null);
            }
        }


        /// <summary>
        ///     加载行政区
        /// </summary>
        private void LoadXZQ()
        {
            var dt = XZQ.SelectQS_JX_XINGZHENGQsDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbXingZhengQ.Items.Add(dt.Rows[i]["S_XINGZHENGQM"].ToString(), dt.Rows[i]["S_XINGZHENGQH"]);
        }


        /// <summary>
        ///     加载环线标记
        /// </summary>
        private void LoadHXBJ()
        {
            cmbHuanXianBJ.Items.Add("外环线外", 2);
            cmbHuanXianBJ.Items.Add("外环线内", 1);
        }


        /// <summary>
        ///     加载井标记
        /// </summary>
        private void LoadJBJ()
        {
            var dt = JBJ.SelectQS_JX_JINGBIAOJXXesDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbJingBJ.Items.Add(dt.Rows[i]["S_YUZHIMC"].ToString(), dt.Rows[i]["S_YUZHIBH"]);
        }

        /// <summary>
        ///     加载流域
        /// </summary>
        private void LoadLY()
        {
            var dt = LYXX.SelectQS_JX_LIUYUXXesDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbLiuYu.Items.Add(dt.Rows[i]["S_LIUYUMC"].ToString(), dt.Rows[i]["S_LIUYUBH"]);
        }

        /// <summary>
        ///     加载水利分片
        /// </summary>
        private void LoadSLFP()
        {
            var dt = SLFP.SelectQS_JX_SHUILIFPXXesDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbShuiLiFP.Items.Add(dt.Rows[i]["S_SHUILIFPMC"].ToString(), dt.Rows[i]["S_SHUILIFPBH"]);
        }

        /// <summary>
        ///     加载水资源分区
        /// </summary>
        private void LoadSZYFQ()
        {
            var dt = SZYFQ.SelectQS_JX_SHUIZIYFQsDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbShuiZiYFQ.Items.Add(dt.Rows[i]["S_SHUIZIYMC"].ToString(), dt.Rows[i]["S_SHUIZIYBH"]);
        }


        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
        }

        /// <summary>
        ///     用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (Status)
            {
                case GridViewStatus.Init:
                    btnEdit.Visible = true;
                    btnEdit.Enabled = true;
                    btnSave.Visible = false;
                    btnSLPC.Visible = false;
                    btnCancel.Visible = false;
                    txtShenJingBH.ClientEnabled = true;
                    txtShenJingBH.Focus();
                    InitControl();
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnSLPC.Visible = true;
                    btnCancel.Visible = true;
                    txtShenJingBH.ClientEnabled = false;
                    SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnSLPC.Visible = true;
                    btnCancel.Visible = true;
                    SetText(true);
                    break;
            }
        }


        /// <summary>
        ///     设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            txtYuanShenJBH.ClientEnabled = b;
            txtQuShuiDD.ClientEnabled = b;
            cmbXingZhengQ.ClientEnabled = b;
            cmbLiuYu.ClientEnabled = b;
            cmbShuiZiYFQ.ClientEnabled = b;
            cmbShuiLiFP.ClientEnabled = b;
            txtCengCi.ClientEnabled = b;
            txtShenDu.ClientEnabled = b;
            txtKouJing.ClientEnabled = b;
            txtXuKeZ.ClientEnabled = b;
            txtDu.ClientEnabled = b;
            txtFen.ClientEnabled = b;
            txtMiao.ClientEnabled = b;
            txtDu2.ClientEnabled = b;
            txtFen2.ClientEnabled = b;
            txtMiao2.ClientEnabled = b;
            cmbJingBJ.ClientEnabled = b;
            cmbHuanXianBJ.ClientEnabled = b;
            txtBeiZhu.ClientEnabled = b;
            txtXuKeSLH.ClientEnabled = b;
            deRiQi_QS.ClientEnabled = b;
            deRiQi_ZZ.ClientEnabled = b;
            cmbJingLei.ClientEnabled = b;
           
        }

        /// <summary>
        ///     保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //修改操作
            operateState = OperateState.Modify;
            /*if (new SqlHelper().P_QS_INSERT_SHENJIN_HIS(JINGXXID, LoginInfo.CurrentUser.UserID.ToString(),null))
            {*/
            int? resmsg = 0;
            _spHelper.P_QSYH_YZXX(2, 2, this.txtShenJingBH.Text, out resmsg);
            if (resmsg == 0)
            {
                _spHelper.P_QS_INSERT_SHENJIN_HIS(this.txtShenJingBH.Text, LoginInfo.CurrentUser.UserID.ToString());
            }
            //////////

            //记录ID 只是用于记录修改日志
            _SJXX = _SJXXBLL.SelectQS_YH_SHENJINGXXB(JINGXXID);

            _SJXX.S_LAOBIANH = txtYuanShenJBH.Text;
            _SJXX.S_JINGDIZ = txtQuShuiDD.Text;
            _SJXX.S_XINGZHENGQBH = cmbXingZhengQ.Value.ToString();
            _SJXX.S_LIUYUBH = cmbLiuYu.Value.ToString();
            _SJXX.S_SHUIZIYBH = cmbShuiZiYFQ.Value.ToString();
            _SJXX.S_FENGPIANBH = cmbShuiLiFP.Value.ToString();
            _SJXX.S_HUANGXIANBH = cmbHuanXianBJ.Value.ToString();
            _SJXX.I_CENG = Convert.ToDecimal(txtCengCi.Text);
            _SJXX.S_SHENGDU = txtShenDu.Text;
            _SJXX.I_KONGJING = Convert.ToDecimal(txtKouJing.Text);
            _SJXX.S_XUKEZHENGBH = txtXuKeZ.Text;
            var zb = txtDu.Text + "/" + txtFen.Text + "/" + txtMiao.Text + "/" + txtDu2.Text + "/" + txtFen2.Text +
                     "/" + txtMiao2.Text;
            _SJXX.S_ZHUOBIAO = zb;
            _SJXX.S_JINGBIAOSBH = cmbJingBJ.Value.ToString();
            _SJXX.S_HUANGXIANBH = cmbHuanXianBJ.Value.ToString();
            //string bz = txtBeiZhu.Text;
            _SJXX.S_BEIZHU = txtBeiZhu.Text;

            _SJXX.S_JINGLEIXBH = cmbJingLei.Text != "" ? cmbJingLei.Value.ToString() : null;
            // _SJXX.S_JINGZHUANGTBH = cmbJingXZ.Value.ToString();
            _SJXX.S_XUKESLH = txtXuKeSLH.Text != "" ? txtXuKeSLH.Text : "";
            _SJXX.D_XUKEQSSJ = deRiQi_QS.Text != "" ? (DateTime?)Convert.ToDateTime(deRiQi_QS.Text) : null;
            _SJXX.D_XUKEJSSJ = deRiQi_ZZ.Text != "" ? (DateTime?)Convert.ToDateTime(deRiQi_ZZ.Text) : null;

            _SYS_LOGBLL.Log(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-井信息维护", operateState, _SJXX,
                null);
            var result = _SJXXBLL.InsertOrUpdate(_SJXX);
            if (result)
            {
                PageTools.AlertMsg(this, "保存成功！");
                Status = GridViewStatus.Init;
                Display();
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
            /* }
             else
             {
                 PageTools.AlertMsg(this, "保存失败！");
             }*/

        }

        /// <summary>
        ///     给输入框赋值
        /// </summary>
        private void SetData()
        {
            _SJXX = _SJXXBLL.SelectQS_YH_SHENJINGXXB(JINGXXID);
            var dtyh = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(
                string.Format(
                    "s_yonghuh =(select s_yonghubh from QS_YH_YONGHUSJGXB where s_jingbianh='{0}' and i_jlzt=0) and i_jlzt=0",
                    _SJXX.S_JINGBH), null);
            txtYongHuH.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUH"].ToString() : "";
            txtYONGHUM.Text = dtyh.Rows.Count > 0 ? dtyh.Rows[0]["S_YONGHUM"].ToString() : "";
            txtYuanShenJBH.Text = _SJXX.S_LAOBIANH;
            txtQuShuiDD.Text = _SJXX.S_JINGDIZ;
            cmbXingZhengQ.Value = _SJXX.S_XINGZHENGQBH;
            cmbLiuYu.Value = _SJXX.S_LIUYUBH;
            cmbShuiZiYFQ.Value = _SJXX.S_SHUIZIYBH;
            cmbShuiLiFP.Value = _SJXX.S_FENGPIANBH;
            cmbHuanXianBJ.Value = _SJXX.S_HUANGXIANBH;
            txtCengCi.Text = _SJXX.I_CENG.ToString();
            txtShenDu.Text = _SJXX.S_SHENGDU;
            txtKouJing.Text = _SJXX.I_KONGJING.ToString();
            txtXuKeZ.Text = _SJXX.S_XUKEZHENGBH;
            var zb = _SJXX.S_ZHUOBIAO != null ? _SJXX.S_ZHUOBIAO : "";
            if (zb != "")
            {
                var arr = zb.Split('/');
                if (arr.Length > 0)
                {
                    txtDu.Text = arr[0];
                    txtFen.Text = arr[1];
                    txtMiao.Text = arr[2];
                    txtDu2.Text = arr[3];
                    txtFen2.Text = arr[4];
                    txtMiao2.Text = arr[5];
                }
            }

            cmbJingLei.Value = _SJXX.S_JINGLEIXBH;
            cmbJingBJ.Value = _SJXX.S_JINGBIAOSBH;
            cmbHuanXianBJ.Value = _SJXX.S_HUANGXIANBH;
            cmbJingXZ.Value = _SJXX.S_JINGZHUANGTBH;
            txtBeiZhu.Text = _SJXX.S_BEIZHU; // +"最近一次修改时间：" + _SJXX.D_CAOZUORQ;
            txtsj.Text = "最近一次修改时间：" + _SJXX.D_CAOZUORQ;
            txtXuKeSLH.Text = _SJXX.S_XUKESLH;
            deRiQi_QS.Value = _SJXX.D_XUKEQSSJ;
            deRiQi_ZZ.Value = _SJXX.D_XUKEJSSJ;
        }

        /// <summary>
        ///     保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (txtQuShuiDD.Text == "")
            {
                PageTools.AlertMsg(this, "地址不能为空！");
                txtQuShuiDD.Focus();
                return false;
            }
            if (cmbXingZhengQ.Text == "")
            {
                PageTools.AlertMsg(this, "行政区不能为空！");
                cmbXingZhengQ.Focus();
                return false;
            }
            if (cmbLiuYu.Text == "")
            {
                PageTools.AlertMsg(this, "流域不能为空！");
                cmbLiuYu.Focus();
                return false;
            }
            if (cmbShuiZiYFQ.Text == "")
            {
                PageTools.AlertMsg(this, "水资源分区不能为空！");
                cmbShuiZiYFQ.Focus();
                return false;
            }
            if (cmbShuiLiFP.Text == "")
            {
                PageTools.AlertMsg(this, "水利分片不能为空！");
                cmbShuiLiFP.Focus();
                return false;
            }
            if (txtCengCi.Text == "")
            {
                PageTools.AlertMsg(this, "层不能为空！");
                txtCengCi.Focus();
                return false;
            }
            if (txtShenDu.Text == "")
            {
                PageTools.AlertMsg(this, "深度不能为空！");
                txtShenDu.Focus();
                return false;
            }
            if (txtKouJing.Text == "")
            {
                PageTools.AlertMsg(this, "口径不能为空！");
                txtKouJing.Focus();
                return false;
            }
            if (cmbJingBJ.Text == "")
            {
                PageTools.AlertMsg(this, "井标记不能为空！");
                cmbJingBJ.Focus();
                return false;
            }
            return true;
        }

        #endregion
    }
}